package com.lgkj.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.lgkj.R;
import com.lgkj.bean.meitao.OrderListBean;
import com.lgkj.utils.CommonUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2018/8/20 0020.
 */

public class OrderAdapter extends BaseAdapter implements View.OnClickListener {
    private Context context;
    private List<OrderListBean.DataBeanX.DataBean> dataBeans;

    public void setData(List<OrderListBean.DataBeanX.DataBean> data) {
        this.dataBeans = data;
    }

    public OrderAdapter(Context context) {
        this.context = context;
        dataBeans = new ArrayList<>();
    }

    private CallBack callBack;

    public void setCallBack(CallBack back) {
        this.callBack = back;
    }

    @Override
    public int getCount() {
        return dataBeans.size();
    }
    @Override
    public OrderListBean.DataBeanX.DataBean getItem(int i) {
        return dataBeans.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.item_order, null);
            viewHolder = new ViewHolder(view);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }
        viewHolder.tvTitle.setText(dataBeans.get(i).getGoods_name());
        BigDecimal bDouble = new BigDecimal(dataBeans.get(i).getGoods_num());
        BigDecimal bString = new BigDecimal(dataBeans.get(i).getGoods_price());
        BigDecimal totalPrices = bDouble.multiply(bString);
        viewHolder.tvPrice.setText("￥：" +totalPrices.setScale(2, RoundingMode.HALF_UP));
        String url = dataBeans.get(i).getCover_id();
        CommonUtils.LoadImage(context, viewHolder.ivImg, url, R.mipmap.shop_item_defult);
        viewHolder.ivPay.setOnClickListener(this);
        viewHolder.ivPay.setTag(i);
        if (dataBeans.get(i).getPay_status().equals("0")) {
            viewHolder.ivPay.setText("去付款");
        } else {
            viewHolder.ivPay.setText(payStatus(dataBeans.get(i).getOrder_status()));
        }
        return view;
    }

    private String payStatus(String status) {
        int staus = 0;
        try {
            staus = Integer.parseInt(status);
        } catch (Exception e) {
        }
        String text = "去付款";
        //0未发货 1已发货 2已收货 3已评价）
        switch (staus) {
            case 0:
                text = "催单";
                break;
            case 1:
                text = "查看物流";
                break;
            case 2:
                text = "去评价";
                break;
        }
        return text;
    }

    @Override
    public void onClick(View view) {
        int position = Integer.parseInt(view.getTag().toString());
        switch (view.getId()) {
            case R.id.iv_pay:
                callBack.gopay(position);
                break;
        }
    }


    public interface CallBack {
        void gopay(int i);

    }

    static class ViewHolder {
        @BindView(R.id.iv_img)
        ImageView ivImg;
        @BindView(R.id.tv_title)
        TextView tvTitle;
        @BindView(R.id.tv_price)
        TextView tvPrice;
        @BindView(R.id.iv_pay)
        TextView ivPay;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
